﻿using justice.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace justice.Controllers
{
    [Authorize]
    public class InsererController : Controller
    {
        // GET: Inserer   保险公司
        justiceEntities dal = new justiceEntities();
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(int? pageNum)
        {
            Dictionary<List<inserer>, int[]> _list = Page.Page<inserer>.Data2(dal.inserer.ToList().ToList(), pageNum);
            ViewBag.InsererList = _list.ElementAt(0).Key;
            ViewBag.PageNum = _list.ElementAt(0).Value[0];
            ViewBag.PageSize = _list.ElementAt(0).Value[1];
            ViewBag.PageCount = _list.ElementAt(0).Value[2];
            return View();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public ActionResult AddInsererView()
        {
            return View();
        }
        public ActionResult AddInserer(inserer inserer)
        {
            dal.inserer.Add(inserer);
            if (dal.SaveChanges() > 0)
            {
                return RedirectToAction("Index", "Inserer");
            }
            return View("AddInsererView");
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult ModInsererView(int id)
        {
            inserer inserer = dal.inserer.ToList().Find(l => l.insurer_id == id);
            return View(inserer); 
        }
        public ActionResult ModInserer(inserer inserer)
        {
            inserer _inserer = dal.inserer.ToList().Find(l => l.insurer_id == inserer.insurer_id);
            _inserer.insurerName = inserer.insurerName;
            _inserer.phone = inserer.phone;
            _inserer.readdress = inserer.readdress;
            if (dal.SaveChanges() > 0)
            {
                return RedirectToAction("Index", "Inserer");
            }
            return View("ModInsererView");
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult DelInserer(int id)
        {
            inserer inserer = dal.inserer.ToList().Find(l => l.insurer_id == id);
            if (inserer != null)
            {
                //此处一系列处理  外键关系
                List<justice_user> userList = dal.justice_user.ToList().FindAll(l => l.insurer_id == id);//查公司下有无用户
                if (userList.Count > 0)
                {
                    return Json("此公司下尚存在用户");
                }
                else
                {
                    List<Shroff_book> shfroff_book_list = dal.Shroff_book.ToList().FindAll(l => l.insurer == id);//查委托书涉及保险公司
                    if (shfroff_book_list.Count > 0)
                    {
                        return Json("此公司已涉及保险");
                    }
                }
                //无外键关系  直接删除
                dal.inserer.Remove(inserer);
            }
            if (dal.SaveChanges() > 0)
                return Json("成功");
            else
                return Json("服务异常");
        }
    }
}